  
package com.xebest.web.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.xebest.common.enums.erp.purch.VenAddTypeEnum;
import com.xebest.common.enums.erp.purch.VenPriceTypeEnum;
import com.xebest.common.enums.erp.purch.VenPurchCheckEnum;
import com.xebest.common.enums.erp.purch.VenPurchStatusEnum;
import com.xebest.common.enums.erp.purch.VenPurchTypeEnum;
import com.xebest.common.enums.erp.purch.VenSettlementMethodEnum;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.controller.export.XeVenPurchExportDTO;
import com.xebest.web.form.XeVenPurchForm;
import com.xebest.web.mapper.read.XeVenPurchCenterExportMapper;
import com.xebest.web.model.XeVenPurchOrderEntity;
import com.xebest.web.service.XeVenPurchCenterExportService;

/**
 * 采购中心导出
 * @desc: xebest-erp  
 * @author: 赵盼飞  
 * @createTime: 2017年6月27日 下午2:57:05  
 * @history:  
 * @version: v1.0
 */
@Service
public class XeVenPurchCenterExportServiceImpl implements XeVenPurchCenterExportService {

	@Autowired
	private XeVenPurchCenterExportMapper xeVenPurchCenterExportMapper;

	/**
	 * 导出采购单列表
	 */
	@Override
	public List<XeVenPurchExportDTO> queryPurchExportList(XeVenPurchForm xeVenPurchForm) {
		
		//组装参数
		XeVenPurchOrderEntity entity = conFormToEntity(xeVenPurchForm);
		List<XeVenPurchExportDTO> purchExportList = xeVenPurchCenterExportMapper.queryPurchExportList(entity);
		for(XeVenPurchExportDTO purch: purchExportList){
			//数字转字符串,结算方式
			SysSetBeanUtils.codeReversionStr("settlementMethod", purch, VenSettlementMethodEnum.class);
			//数字转字符串,审核状态
			SysSetBeanUtils.codeReversionStr("checkStatus", purch, VenPurchCheckEnum.class);
			//数字转字符串,采购单状态
			SysSetBeanUtils.codeReversionStr("purchStatus", purch, VenPurchStatusEnum.class);
			//数字转字符串,采购类型
			SysSetBeanUtils.codeReversionStr("purchType", purch, VenPurchTypeEnum.class);
			//数字转字符串,生产方式
			SysSetBeanUtils.codeReversionStr("addType", purch, VenAddTypeEnum.class);
		}
		return purchExportList;  
	}

	/**
	 * 导出采购单明细
	 */
	@Override
	public List<XeVenPurchExportDTO> queryPurchDetailExportList(XeVenPurchForm form) {
		XeVenPurchOrderEntity entity = conFormToEntity(form);
		List<XeVenPurchExportDTO> purchDetailList = xeVenPurchCenterExportMapper.queryPurchDetailExportList(entity);
		for(XeVenPurchExportDTO purch: purchDetailList){
			//数字转字符串,结算方式
			SysSetBeanUtils.codeReversionStr("settlementMethod", purch, VenSettlementMethodEnum.class);
			//数字转字符串,审核状态
			SysSetBeanUtils.codeReversionStr("checkStatus", purch, VenPurchCheckEnum.class);
			//数字转字符串,采购单状态
			SysSetBeanUtils.codeReversionStr("purchStatus", purch, VenPurchStatusEnum.class);
			//数字转字符串,采购类型
			SysSetBeanUtils.codeReversionStr("purchType", purch, VenPurchTypeEnum.class);
			//数字转字符串,计价方式
			SysSetBeanUtils.codeReversionStr("priceType", purch, VenPriceTypeEnum.class);
			//数字转字符串,生产方式
			SysSetBeanUtils.codeReversionStr("addType", purch, VenAddTypeEnum.class);
		}
		
		return purchDetailList;  
	}
	
	
	public XeVenPurchOrderEntity conFormToEntity(XeVenPurchForm xeVenPurchForm){
		XeVenPurchOrderEntity purchEntity = new XeVenPurchOrderEntity();
		if(null != xeVenPurchForm ){
			purchEntity = JSONObject.parseObject(JSONObject.toJSONString(xeVenPurchForm), XeVenPurchOrderEntity.class);
			if(null != xeVenPurchForm.getSearchType()
				&& StringUtils.isNotBlank(xeVenPurchForm.getSearchText())){
				if(new Integer(1).equals(xeVenPurchForm.getSearchType())){
					purchEntity.setPurchOrder(xeVenPurchForm.getSearchText());
				}else if(new Integer(2).equals(xeVenPurchForm.getSearchType())){
					purchEntity.setSupplierName(xeVenPurchForm.getSearchText());
				}else if(new Integer(3).equals(xeVenPurchForm.getSearchType())){
					purchEntity.setRemark(xeVenPurchForm.getSearchText());
				}else if(new Integer(4).equals(xeVenPurchForm.getSearchType())){
					purchEntity.setStoreName(xeVenPurchForm.getSearchText());
				}else if(new Integer(5).equals(xeVenPurchForm.getSearchType())){
					purchEntity.setOrderIdSearch(xeVenPurchForm.getSearchText());
				}
			}
			
			if(null != xeVenPurchForm.getSearchTimeType()
				&& StringUtils.isNotBlank(xeVenPurchForm.getSearchTimeStart())
				&& StringUtils.isNotBlank(xeVenPurchForm.getSearchTimeEnd())){
				if(new Integer(1).equals(xeVenPurchForm.getSearchTimeType())){
					purchEntity.setSignDateStart(xeVenPurchForm.getSearchTimeStart());
					purchEntity.setSignDateEnd(xeVenPurchForm.getSearchTimeEnd());
				}else if(new Integer(2).equals(xeVenPurchForm.getSearchTimeType())){
					purchEntity.setExpectedArrivalDateStart(xeVenPurchForm.getSearchTimeStart());
					purchEntity.setExpectedArrivalDateEnd(xeVenPurchForm.getSearchTimeEnd());
				}else if(new Integer(3).equals(xeVenPurchForm.getSearchTimeType())){
					purchEntity.setCompleteDateStart(xeVenPurchForm.getSearchTimeStart());
					purchEntity.setCompleteDateEnd(xeVenPurchForm.getSearchTimeEnd());
				}
			}
		}
		return purchEntity;
	}
	
	
}
  
